import EventManager from "../core/EventManager";
import ResManager from "../core/ResManager";
import { CommonEvents } from "../net/CommonEvents";
import Singleton from "../net/Singleton";
import LogUtil from "../utils/LogUtil";
import { StorageUtil } from "../utils/StorageUtil";

/** 引导数据 */
export default class GuideData extends Singleton {
    /** 当前指引配置 */
    private guideCfb:any;
    private groupId:number;
    private smallId:number;
    private callback:Function;

    /**
     * 初始化引导
     * @param guideData 存档数据
     */
    init(guideData){
        LogUtil.log("guideData>>",guideData);
        this.groupId = (guideData.groupId || 0) + 1;
        this.smallId = 0;
        this.getNextConfig();
    }

    getGroupId(){
        return this.groupId;
    }

    checkHasGuide(groupId: number,smallId: number){
        let config_guide = ResManager.ins().getRes('config_guide');
        if (config_guide[groupId] && config_guide[groupId][smallId]) {
            return true;
        }
        LogUtil.log('当前引导不存在>>>', this.groupId, this.smallId);
        return false;
    }
    
    getNextConfig(){
        let config_guide = ResManager.ins().getRes('config_guide');
        // LogUtil.log("config_guide>>>",config_guide);
        if (config_guide[this.groupId]) {
            if (config_guide[this.groupId][this.smallId + 1]) {
                this.smallId++;
                if (config_guide[this.groupId][this.smallId]) {
                    this.guideCfb = config_guide[this.groupId][this.smallId];
                    // cc.warn('==========当前引导', this.groupId, this.smallId)
                } else {
                    this.guideCfb = null;
                }
            } else {
                this.groupId++;
                this.smallId = 1;
                if (config_guide[this.groupId] && config_guide[this.groupId][this.smallId]) {
                    this.guideCfb = config_guide[this.groupId][this.smallId];
                    // cc.warn('==========当前引导', this.groupId, this.smallId)
                } else {
                    this.guideCfb = null;
                }
            }
        } else {
            this.guideCfb = null;
        }
    }

    /**
     * 检测引导触发
     * @param type 触发类型
     * @param callback 引导完成回调
     * @param pageName 页面名称
     * @returns 
     */
    check(type:number, callback?:Function , pageName?:string) {
        if (this.guideCfb
            && this.guideCfb.triggerParameter == type
            && (!this.guideCfb.pageName || this.guideCfb.pageName == pageName)) {
            if (callback) {
                this.callback = callback;
            }
            LogUtil.warn('------------触发了引导', this.groupId, this.smallId);
            EventManager.emit(CommonEvents.GUIDE_SHOW, this.groupId, this.smallId);
            return false;
        }
        return true;
    }
    
    /**
     * 跳过引导
     */
    pass() {
        if (this.guideCfb) {
            //保存存档数据
            // StorageUtil.ins().setItem('local_groupId', this.guideCfb.groupId);
            
            EventManager.emit(CommonEvents.GUIDE_CLOSE);
            let config_guide = ResManager.ins().getRes('config_guide');
            if (config_guide[this.groupId + 1] && config_guide[this.groupId + 1][1]) {
                this.groupId++;
                this.smallId = 1;
                this.guideCfb = config_guide[this.groupId][this.smallId];
            } else {
                this.guideCfb = null;
            }
        }
    }

    /**
     * 引导结束
     * @param completeWay 完成参数
     */
    complete(completeWay:number) {
        if (this.guideCfb) {
            if (completeWay == this.guideCfb.completeWay) {
                if (this.guideCfb.isEnd == 1) {
                    //保存存档数据
                    // StorageUtil.ins().setItem('local_groupId', this.guideCfb.groupId);
                }
                EventManager.emit(CommonEvents.GUIDE_CLOSE);
                this.getNextConfig();
                let bool = this.check(-1);
                if (bool && this.callback) {
                    this.callback();
                    this.callback = null;
                }
            }
        }
    }
}